package com.atguigu.tingshu.user.pattern.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.pattern.UserPaidStrategy;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author: atguigu
 * @create: 2024-04-23 14:48
 */
@Slf4j
@Component(SystemConstant.ORDER_ITEM_TYPE_VIP)  //beanid=类名（首字母小写）
public class VIPPaidStrategy implements UserPaidStrategy {

    @Autowired
    private VipServiceConfigMapper vipServiceConfigMapper;

    @Autowired
    private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;


    /**
     * 处理VIP会员购买记录
     *
     * @param userPaidRecordVo
     */
    @Override
    public void handlerPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        log.info("处理VIP会员购买记录");
        //3.处理购买项目类型为：VIP会员
        Long vipConfigId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipConfigId);
        //得到会员月
        Integer serviceMonth = vipServiceConfig.getServiceMonth();
        //3.1 获取当前用户身份，确定新开会员，续费会员
        Boolean isVIP = false;
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        if (userInfo.getIsVip().intValue() == 1 && userInfo.getVipExpireTime().after(new Date())) {
            //当前用户是VIP
            isVIP = true;
        }
        //3.2 创建VIP购买记录将记录保存
        UserVipService userVipService = new UserVipService();
        //3.2.1 计算本次会员开始及过期时间
        if (isVIP) {
            //续费会员，本次会员开始时间=用户表中会员过期时间
            userVipService.setStartTime(userInfo.getVipExpireTime());
            userVipService.setExpireTime(DateUtil.offsetMonth(userInfo.getVipExpireTime(), serviceMonth));
        } else {
            //新开会员
            userVipService.setStartTime(new Date());
            userVipService.setExpireTime(DateUtil.offsetMonth(new Date(), serviceMonth));
        }
        //3.2.2 封装其他属性
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
        userVipService.setUserId(userPaidRecordVo.getUserId());
        //3.2.3 保存VIP购买记录
        userVipServiceMapper.insert(userVipService);

        //3.3 更新用户会员标识及过期时间
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(userVipService.getExpireTime());
        userInfoMapper.updateById(userInfo);
    }
}
